<template>
  <p-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-form-item label="检验项目编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'inspectProjectCode', validatorRules.inspectProjectCode]" placeholder="请输入检验项目编码"></p-input>
        </p-form-item>
        <p-form-item label="检验项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'inspectProjectName', validatorRules.inspectProjectName]" placeholder="请输入检验项目名称"></p-input>
        </p-form-item>
        <p-form-item label="检验标准" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'inspectStandard', validatorRules.inspectStandard]" placeholder="请输入检验标准"></p-input>
        </p-form-item>
        <p-form-item label="检验值类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['inspectValueType', validatorRules.inspectValueType]" :trigger-change="true" dictCode="inspect_value_type" placeholder="请选择检验值类型"/>
        </p-form-item>
        <p-form-item label="检验手段" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'inspectMethod', validatorRules.inspectMethod]" placeholder="请输入检验手段"></p-input>
        </p-form-item>
        <p-form-item label="项目分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['projectClassify', validatorRules.projectClassify]" :trigger-change="true" dictCode="mes_project_classify_type" placeholder="请选择项目分类"/>
        </p-form-item>
        <p-form-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['projectType', validatorRules.projectType]" :trigger-change="true" dictCode="project_type" placeholder="请选择项目类型"/>
        </p-form-item>

      </p-form>
    </p-spin>
    <p-button type="primary" @click="handleOk">确定</p-button>
    <p-button type="primary" @click="handleCancel">取消</p-button>
  </p-drawer>
</template>

<script>

  import { httpAction, putAction, api } from '@/api/manage'
  import pick from 'lodash.pick'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "MesQcInspectIndexLibraryModal",
	mixins: [SubmitOnchangMixin],
    components: { 
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          inspectProjectCode: {rules: [
            {required: true, message: '请输入检验项目编码!'},
          ]},
          inspectProjectName: {rules: [
            {required: true, message: '请输入检验项目名称!'},
          ]},
          inspectStandard: {rules: [
            {required: true, message: '请输入检验标准!'},
          ]},
          inspectValueType: {rules: [
            {required: true, message: '请输入检验值类型!'},
          ]},
          inspectMethod: {rules: [
            {required: true, message: '请输入检验手段!'},
          ]},
          projectClassify: {rules: [
            {required: true, message: '请输入项目分类!'},
          ]},
          projectType: {rules: [
            {required: true, message: '请输入项目类型!'},
          ]},
        },
        url: {
          add: '/mes-quality/mesQcInspectIndexLibrary/add',
          edit: '/mes-quality/mesQcInspectIndexLibrary/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
        this.getRuleCodeValue();
      },

      getRuleCodeValue() {
        let param = {
          commonFormData: this.model,
          rules: [
            {"ruleCode": "mes_inspect_project_rule", "columnName": "inspectProjectCode"},
            ]
        }

        putAction(api.ruleBaseURL, param).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            console.log("生成的编码：" + res.data)
            const item = res.data;
            this.form.setFieldsValue(pick(item, Object.keys(item)))
          }
        })
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'inspectProjectCode','inspectProjectName','inspectStandard','inspectValueType','inspectMethod','projectClassify','projectType','createBy','createTime','updateBy','updateTime','status','remark'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'inspectProjectCode','inspectProjectName','inspectStandard','inspectValueType','inspectMethod','projectClassify','projectType','createBy','createTime','updateBy','updateTime','status','remark'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>
